package cn.stylefeng.guns.modular.tool.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.ExcelWrite;
import cn.stylefeng.guns.modular.tool.entity.ToolExpressReconciliationSf;
import cn.stylefeng.guns.modular.tool.mapper.ToolExpressReconciliationSfMapper;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressReconciliationRfParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressReconciliationRyyInstallParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressReconciliationSfParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressReconciliationSfbkInstallParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressReconciliationSfkhInstallParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressReconciliationSftkInstallParam;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressReconciliationRyyInstallResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressReconciliationSfResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressReconciliationSfbkInstallResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressReconciliationSfkhInstallResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressReconciliationSftkInstallResult;
import cn.stylefeng.guns.modular.tool.service.ToolExpressReconciliationRfService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressReconciliationRyyInstallService;
import  cn.stylefeng.guns.modular.tool.service.ToolExpressReconciliationSfService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressReconciliationSfbkInstallService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressReconciliationSfkhInstallService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressReconciliationSftkInstallService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * <p>
 * 顺丰对账管理 服务实现类
 * </p>
 * @author zhucx
 * @since 2023-07-22
 */
@Service
public class ToolExpressReconciliationSfServiceImpl extends ServiceImpl<ToolExpressReconciliationSfMapper, ToolExpressReconciliationSf> implements ToolExpressReconciliationSfService {

    @Override
    public void add(ToolExpressReconciliationSfParam param){
        ToolExpressReconciliationSf entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ToolExpressReconciliationSfParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ToolExpressReconciliationSfParam param){
        ToolExpressReconciliationSf oldEntity = getOldEntity(param);
        ToolExpressReconciliationSf newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ToolExpressReconciliationSfResult findBySpec(ToolExpressReconciliationSfParam param){
        return null;
    }

    @Override
    public List<ToolExpressReconciliationSfResult> findListBySpec(ToolExpressReconciliationSfParam param){
        return this.baseMapper.findListBySpec(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ToolExpressReconciliationSfParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecSfDz(ToolExpressReconciliationSfParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findPageBySpecSfDz(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ToolExpressReconciliationSfParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ToolExpressReconciliationSf getOldEntity(ToolExpressReconciliationSfParam param) {
        return this.getById(getKey(param));
    }

    private ToolExpressReconciliationSf getEntity(ToolExpressReconciliationSfParam param) {
        ToolExpressReconciliationSf entity = new ToolExpressReconciliationSf();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
    
    @Resource
    private UserService userService;

    @Resource
    private ToolExpressReconciliationSftkInstallService toolExpressReconciliationSftkInstallService;

    @Resource
    private ToolExpressReconciliationSfkhInstallService toolExpressReconciliationSfkhInstallService;

    @Resource
    private ToolExpressReconciliationSfbkInstallService toolExpressReconciliationSfbkInstallService;

    @Resource
    private ToolExpressReconciliationRyyInstallService toolExpressReconciliationRyyInstallService;

    @Override
	public ResponseData getExceldata(MultipartFile file,Long reconciliationId, String zk, ResponseData data) throws IOException{
		String fileName = file.getOriginalFilename();
		
		User user = userService.getcurrentUser();

    	BigDecimal as = new BigDecimal("0");
    	BigDecimal ldjiusi = new BigDecimal("0.94");
    	BigDecimal yi = new BigDecimal("1");
    	BigDecimal liu = new BigDecimal("6");
    	BigDecimal shi = new BigDecimal("10");
    	BigDecimal ershi = new BigDecimal("20");
    	BigDecimal sanshi = new BigDecimal("30");
    	BigDecimal liushi = new BigDecimal("60");
		
		List<ToolExpressReconciliationSfParam> sflist = new ArrayList<ToolExpressReconciliationSfParam>();
		List<ToolExpressReconciliationRfParam> rflist = new ArrayList<ToolExpressReconciliationRfParam>();
		
		ToolExpressReconciliationSftkInstallParam sftkInstallParam = new ToolExpressReconciliationSftkInstallParam();
		sftkInstallParam.setDeptId(user.getDeptId());
		List<ToolExpressReconciliationSftkInstallResult> tki = toolExpressReconciliationSftkInstallService.findListBySpec(sftkInstallParam, user.getOperateId());//顺丰特快 成本报价
		
		ToolExpressReconciliationSfkhInstallParam sfkhInstallParam = new ToolExpressReconciliationSfkhInstallParam();
		sfkhInstallParam.setDeptId(user.getDeptId());
		List<ToolExpressReconciliationSfkhInstallResult> khi = toolExpressReconciliationSfkhInstallService.findListBySpec(sfkhInstallParam, user.getOperateId());//顺丰卡航 成本报价
		
		ToolExpressReconciliationSfbkInstallParam sfbkInstallParam = new ToolExpressReconciliationSfbkInstallParam();
		sfbkInstallParam.setDeptId(user.getDeptId());
		List<ToolExpressReconciliationSfbkInstallResult> bki = toolExpressReconciliationSfbkInstallService.findListBySpec(sfbkInstallParam, user.getOperateId());//顺丰标快 成本报价
		
		ToolExpressReconciliationRyyInstallParam ryyInstallParam = new ToolExpressReconciliationRyyInstallParam();
		ryyInstallParam.setDeptId(user.getDeptId());
		List<ToolExpressReconciliationRyyInstallResult> ryyi = toolExpressReconciliationRyyInstallService.findListBySpec(ryyInstallParam, user.getOperateId());//润月雅 收入报价

		if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
			data.setMessage("上传文件格式不正确");
			data.setSuccess(false);
			return data;
        }

		boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        
        for (int r = 1; r <= sheet.getLastRowNum()+1; r++) {
        	Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }
            ToolExpressReconciliationSfParam sffff = new ToolExpressReconciliationSfParam();//顺丰账单
            sffff.setReconciliationId(reconciliationId);
            
            ToolExpressReconciliationRfParam rfffParam = new ToolExpressReconciliationRfParam();//软风账单
            rfffParam.setV1("sf");
            rfffParam.setReconciliationId(reconciliationId);

            String  noNumber = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(0));//序号
            if (ToolUtil.isNotEmpty(noNumber)) {
            	sffff.setNoNumber(noNumber);
			} 

            String  receivingTime = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(1));//日期
            if (ToolUtil.isNotEmpty(receivingTime)) {
            	sffff.setReceivingTime(receivingTime);
			} 

            String  orderNumber = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(2));//邮件号
            if (ToolUtil.isNotEmpty(orderNumber)) {
            	sffff.setOrderNumber(orderNumber);
            	rfffParam.setOrderNumber(orderNumber);
			} else {
				break;
			}
            
            BigDecimal aWeight = new BigDecimal("0");//核算重量
            String aProvince = "";//核算地区

            String  reachArea = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(3));//到件地区
            if (ToolUtil.isNotEmpty(reachArea)) {
            	aProvince = reachArea;
            	sffff.setReachArea(reachArea);
            	rfffParam.setProvince(reachArea);
			} 

            String  duifang = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(4));//对方公司名称
            if (ToolUtil.isNotEmpty(duifang)) {
            	sffff.setDuifang(duifang);
			} 

            String  chargingWeight = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(5));//计费重量
            if (ToolUtil.isNotEmpty(chargingWeight)) {
            	BigDecimal chargingWeightbig = new BigDecimal(chargingWeight).setScale(2, BigDecimal.ROUND_HALF_UP);
            	aWeight = chargingWeightbig;
            	sffff.setChargingWeight(chargingWeightbig.toString());

				if(chargingWeightbig.compareTo(as) == 1 && chargingWeightbig.compareTo(shi) != 1) {//10KG以下
					sffff.setWeightQj("10KG以下");
					sffff.setWeightQj2("10KG以下");
					rfffParam.setWeightQj("10KG以下");
				}
				if(chargingWeightbig.compareTo(shi) == 1 && chargingWeightbig.compareTo(ershi) != 1) {//10 -  20KG
					sffff.setWeightQj("10-20KG");
					sffff.setWeightQj2("10-20KG");
					rfffParam.setWeightQj("10-20KG");
				}
				if(chargingWeightbig.compareTo(ershi) == 1 && chargingWeightbig.compareTo(liushi) != 1) {//20 -  60KG
					sffff.setWeightQj("20-60KG");
					sffff.setWeightQj2("20-60KG");
					rfffParam.setWeightQj("20-60KG");
				}
				if(chargingWeightbig.compareTo(liushi) == 1) {//60KG以上
					sffff.setWeightQj("60KG以上");
					sffff.setWeightQj2("60KG以上");
					rfffParam.setWeightQj("60KG以上");
				}
			} 

            String  product = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(6));//产品
            if (ToolUtil.isNotEmpty(product)) {
            	sffff.setProduct(product);
			} 

            String  fukuantype = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(7));//付款方式
            if (ToolUtil.isNotEmpty(fukuantype)) {
            	sffff.setFukuantype(fukuantype);
			} 

            String  feiyong = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(8));//费用
            if (ToolUtil.isNotEmpty(feiyong)) {
            	BigDecimal feiyongbig = new BigDecimal(feiyong).setScale(2, BigDecimal.ROUND_HALF_UP);
            	sffff.setFeiyong(feiyongbig.toString());
			} 

            String  discount = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(9));//折扣
            if (ToolUtil.isNotEmpty(discount)) {
            	BigDecimal discountbig = new BigDecimal(discount).setScale(2, BigDecimal.ROUND_HALF_UP);
            	sffff.setDiscount(discountbig.toString());
			} 

            String  postage = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(10));//应付金额
            if (ToolUtil.isNotEmpty(postage)) {
            	BigDecimal postagebig = new BigDecimal(postage).setScale(2, BigDecimal.ROUND_HALF_UP);
            	sffff.setPostage(postagebig.toString());
			} 

            String  jingbanren = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(11));//经办人
            if (ToolUtil.isNotEmpty(jingbanren)) {
            	sffff.setJingbanren(jingbanren);
			} 

            String  zengzhi = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(12));//增值服务费
            if (ToolUtil.isNotEmpty(zengzhi)) {
            	BigDecimal zengzhibig = new BigDecimal(zengzhi).setScale(2, BigDecimal.ROUND_HALF_UP);
            	sffff.setZengzhi(zengzhibig.toString());
            	sffff.setZengzhi2(zengzhibig.toString());
			} 

            String  customerName = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(13));//客户
            if (ToolUtil.isNotEmpty(customerName)) {
            	sffff.setCustomerName(customerName);
            	rfffParam.setCustomerName(customerName);
			} else {
				sffff.setCustomerName("无");
            	rfffParam.setCustomerName("无");
			}
            
            if(aWeight.compareTo(as) == 1 && ToolUtil.isNotEmpty(aProvince) && ToolUtil.isNotEmpty(sffff.getProduct())) {
        		//匹配顺丰报价   计算核对邮费  计算邮费差异
            	if(sffff.getProduct().equals("顺丰特快")) {//单价+（重量-1）*续重	1公斤及以下按1公斤
            		for (ToolExpressReconciliationSftkInstallResult i : tki) {
        				if (i.getV1().indexOf(aProvince)!=-1) {
        					sffff.setFirstWeight(i.getUnilateralism());
        					sffff.setWeightContinuation(i.getCWeight());
        					if(aWeight.compareTo(yi) != 1) {
        						sffff.setPostageCheck(i.getUnilateralism());
        					} else {
    							if(ToolUtil.isNotEmpty(i.getUnilateralism()) && ToolUtil.isNotEmpty(i.getCWeight())) {
    								BigDecimal aWeightsub = aWeight.subtract(yi);
    								BigDecimal cWeight = new BigDecimal(i.getCWeight());
    								BigDecimal unilateralism = new BigDecimal(i.getUnilateralism());
    								BigDecimal xmulz = cWeight.multiply(aWeightsub).setScale(4, BigDecimal.ROUND_HALF_UP);
    								BigDecimal postageCheck = xmulz.add(unilateralism).setScale(4, BigDecimal.ROUND_HALF_UP);
            						sffff.setPostageCheck(postageCheck.toString());
    							}
        					}
        					break;
        				}
					}
            	} 
            	if(sffff.getProduct().equals("顺丰卡航")) {
            		for (ToolExpressReconciliationSfkhInstallResult i : khi) {//20-60kg：		首重20+（实际重量-20）*续重		60以上： 	首重60+（实际重量-60）*续重
        				if (i.getCity().indexOf(aProvince)!=-1) {
        					if(aWeight.compareTo(ershi) != -1 && aWeight.compareTo(liushi) != 1) {//20 -  60kg
    							if(ToolUtil.isNotEmpty(i.getFirstWeight26kg()) && ToolUtil.isNotEmpty(i.getCWeight26kg())) {
    	        					sffff.setFirstWeight(i.getFirstWeight26kg());
    	        					sffff.setWeightContinuation(i.getCWeight26kg());
    								BigDecimal aWeightsub = aWeight.subtract(ershi);
    								BigDecimal cWeight26kg = new BigDecimal(i.getCWeight26kg());
    								BigDecimal firstWeight26kg = new BigDecimal(i.getFirstWeight26kg());
    								BigDecimal xmulz = cWeight26kg.multiply(aWeightsub).setScale(4, BigDecimal.ROUND_HALF_UP);
    								BigDecimal postageCheck = xmulz.add(firstWeight26kg).setScale(4, BigDecimal.ROUND_HALF_UP);
            						sffff.setPostageCheck(postageCheck.toString());
            						if(ToolUtil.isNotEmpty(zk)) {
        								BigDecimal zkbig = new BigDecimal(zk);
        								BigDecimal postageCheckm = new BigDecimal("0");
        								if(ToolUtil.isNotEmpty(sffff.getPostage())) {
        									postageCheckm = new BigDecimal(sffff.getPostage());
        								} else {
        									postageCheckm = postageCheck;
        								}
        								BigDecimal postageCheckmulzk = postageCheckm.multiply(zkbig).setScale(4, BigDecimal.ROUND_HALF_UP);
        								BigDecimal postageChecksub = postageCheck.subtract(postageCheckmulzk).setScale(4, BigDecimal.ROUND_HALF_UP);
        								sffff.setDiscount2(postageChecksub.toString());
            						} else {
        								sffff.setDiscount2("0");
            						}
    							}
        					}
        					if(aWeight.compareTo(liushi) == 1) {//60kg以上
    							if(ToolUtil.isNotEmpty(i.getFirstWeight6kg()) && ToolUtil.isNotEmpty(i.getCWeight6kg())) {
    	        					sffff.setFirstWeight(i.getFirstWeight6kg());
    	        					sffff.setWeightContinuation(i.getCWeight6kg());
    								BigDecimal aWeightsub = aWeight.subtract(liushi);
    								BigDecimal cWeight6kg = new BigDecimal(i.getCWeight6kg());
    								BigDecimal firstWeight6kg = new BigDecimal(i.getFirstWeight6kg());
    								BigDecimal xmulz = cWeight6kg.multiply(aWeightsub).setScale(4, BigDecimal.ROUND_HALF_UP);
    								BigDecimal postageCheck = xmulz.add(firstWeight6kg).setScale(4, BigDecimal.ROUND_HALF_UP);
            						sffff.setPostageCheck(postageCheck.toString());
            						if(ToolUtil.isNotEmpty(zk)) {
        								BigDecimal zkbig = new BigDecimal(zk);
        								BigDecimal postageCheckmulzk = postageCheck.multiply(zkbig).setScale(4, BigDecimal.ROUND_HALF_UP);
        								BigDecimal postageChecksub = postageCheck.subtract(postageCheckmulzk).setScale(4, BigDecimal.ROUND_HALF_UP);
        								sffff.setDiscount2(postageChecksub.toString());
            						} else {
        								sffff.setDiscount2("0");
            						}
    							}
        					}
        					break;
        				}
					}
            	}
            	if(sffff.getProduct().equals("顺丰标快")) {
            		for (ToolExpressReconciliationSfbkInstallResult i : bki) {//重量小于30kg：首重+（重量-1）*续重		重量大于等于30kg：首重（重量-1）*最后一列续重
            			if (i.getCity().indexOf(aProvince)!=-1) {
        					if(aWeight.compareTo(as) == 1 && aWeight.compareTo(sanshi) != 1) {//0 -  30kg
    							if(ToolUtil.isNotEmpty(i.getFirstWeight()) && ToolUtil.isNotEmpty(i.getCWeightLt3kg())) {
    	        					sffff.setFirstWeight(i.getFirstWeight());
    	        					sffff.setWeightContinuation(i.getCWeightLt3kg());
    								BigDecimal aWeightsub = aWeight.subtract(yi);
    								BigDecimal cWeight3kg = new BigDecimal(i.getCWeightLt3kg());
    								BigDecimal firstWeight = new BigDecimal(i.getFirstWeight());
    								BigDecimal xmulz = cWeight3kg.multiply(aWeightsub).setScale(4, BigDecimal.ROUND_HALF_UP);
    								BigDecimal postageCheck = xmulz.add(firstWeight).setScale(4, BigDecimal.ROUND_HALF_UP);
            						sffff.setPostageCheck(postageCheck.toString());
    							}
        					}
        					if(aWeight.compareTo(sanshi) == 1) {//30kg以上
    							if(ToolUtil.isNotEmpty(i.getFirstWeight()) && ToolUtil.isNotEmpty(i.getCWeightGt3kg())) {
    	        					sffff.setFirstWeight(i.getFirstWeight());
    	        					sffff.setWeightContinuation(i.getCWeightGt3kg());
    								BigDecimal aWeightsub = aWeight.subtract(yi);
    								BigDecimal cWeight3kg = new BigDecimal(i.getCWeightGt3kg());
    								BigDecimal firstWeight = new BigDecimal(i.getFirstWeight());
    								BigDecimal xmulz = cWeight3kg.multiply(aWeightsub).setScale(4, BigDecimal.ROUND_HALF_UP);
    								BigDecimal postageCheck = xmulz.add(firstWeight).setScale(4, BigDecimal.ROUND_HALF_UP);
            						sffff.setPostageCheck(postageCheck.toString());
    							}
        					}
        					break;
            			}
					}
            	}
            } else {
				sffff.setPostageCheck("0");
            }
            
            if(ToolUtil.isNotEmpty(sffff.getPostageCheck())) {
				BigDecimal postageCheckSum = new BigDecimal(sffff.getPostageCheck());
            	if(ToolUtil.isNotEmpty(sffff.getZengzhi2())) {
            		BigDecimal zengzhi2big = new BigDecimal(sffff.getZengzhi2());
            		postageCheckSum = postageCheckSum.add(zengzhi2big).setScale(4, BigDecimal.ROUND_HALF_UP);
            	}
            	if(ToolUtil.isNotEmpty(sffff.getDiscount2())) {
            		BigDecimal discount2big = new BigDecimal(sffff.getDiscount2());
            		postageCheckSum = postageCheckSum.subtract(discount2big).setScale(4, BigDecimal.ROUND_HALF_UP);
            	}
            	sffff.setPostageCheckSum(postageCheckSum.toString());
            	if(ToolUtil.isNotEmpty(sffff.getPostage())) {
            		BigDecimal postagebig = new BigDecimal(sffff.getPostage());
            		BigDecimal postageDifferencebig = postagebig.subtract(postageCheckSum).setScale(4, BigDecimal.ROUND_HALF_UP);
            		sffff.setPostageDifference(postageDifferencebig.toString());//总差异
            	}
            }

            if(ToolUtil.isNotEmpty(rfffParam.getCustomerName())) {
        		if(aWeight.compareTo(as) == 1 && ToolUtil.isNotEmpty(aProvince)) {
        			rfffParam.setChargingWeightKg(aWeight.toString());//计费重量
        			BigDecimal roundingWeightKg = aWeight.setScale( 0, BigDecimal.ROUND_UP );
    				rfffParam.setRoundingWeightKg(aWeight.setScale( 0, BigDecimal.ROUND_UP ).longValue()+"");//取整重量
                	if(rfffParam.getCustomerName().equals("丝丽")||rfffParam.getCustomerName().equals("医美物料")) {//20+（取整重量-1）*6
						BigDecimal aWeightsub = roundingWeightKg.subtract(yi);
						BigDecimal xmulz = liu.multiply(aWeightsub).setScale(8, BigDecimal.ROUND_HALF_UP);
						BigDecimal expressMoney = xmulz.add(ershi).setScale(8, BigDecimal.ROUND_HALF_UP);
						rfffParam.setFirstWeight("20");
						rfffParam.setCWeight("6");
						rfffParam.setExpressMoney(expressMoney.toString());
						rfffParam.setSumExpressMoney(expressMoney.toString());
						sffff.setIncome(expressMoney.toString());
                		rflist.add(rfffParam);
                	}
                	if(rfffParam.getCustomerName().equals("润月雅")) {
                		for (ToolExpressReconciliationRyyInstallResult i : ryyi) {//单价+（取整重量-1）*续重	1公斤及以下按1公斤
            				if (i.getV1().indexOf(aProvince)!=-1) {
            					rfffParam.setFirstWeight(i.getUnilateralism());
            					rfffParam.setCWeight(i.getCWeight());
            					if(roundingWeightKg.compareTo(yi) != 1) {
            						rfffParam.setExpressMoney(i.getUnilateralism());
            						rfffParam.setSumExpressMoney(i.getUnilateralism());
            						sffff.setIncome(i.getUnilateralism());
            					} else {
        							if(ToolUtil.isNotEmpty(i.getUnilateralism()) && ToolUtil.isNotEmpty(i.getCWeight())) {
        								BigDecimal aWeightsub = roundingWeightKg.subtract(yi);
        								BigDecimal cWeight = new BigDecimal(i.getCWeight());
        								BigDecimal unilateralism = new BigDecimal(i.getUnilateralism());
        								BigDecimal xmulz = cWeight.multiply(aWeightsub).setScale(8, BigDecimal.ROUND_HALF_UP);
        								BigDecimal expressMoney = xmulz.add(unilateralism).setScale(8, BigDecimal.ROUND_HALF_UP);
        								rfffParam.setExpressMoney(expressMoney.toString());
        								rfffParam.setSumExpressMoney(expressMoney.toString());
        								sffff.setIncome(expressMoney.toString());
        							}
            					}
            					break;
            				}
						}
                		rflist.add(rfffParam);
                	}
        		}
            }

            if(ToolUtil.isNotEmpty(rfffParam.getSumExpressMoney())) {
            	BigDecimal sumExpressMoney = new BigDecimal(rfffParam.getSumExpressMoney());//收入
            	BigDecimal postagebig = new BigDecimal("0");//成本
            	if(ToolUtil.isNotEmpty(sffff.getPostage())) {
            		postagebig = new BigDecimal(sffff.getPostage());
            	} 
            	
            	BigDecimal profit = sumExpressMoney.subtract(postagebig).setScale(4,BigDecimal.ROUND_HALF_UP);//收入  -  成本 = 毛利
            	sffff.setProfit(profit.toString());
            	
            	BigDecimal qssumExpressMoney = sumExpressMoney.multiply(ldjiusi).setScale(4, BigDecimal.ROUND_HALF_UP);//收入 * 0.94 = 去税收入
            	BigDecimal profitqs = qssumExpressMoney.subtract(postagebig).setScale(4,BigDecimal.ROUND_HALF_UP);//去税收入  -  成本 = 去税利润
            	sffff.setProfitqs(profitqs.toString());
            }
            sflist.add(sffff);
        }
        if(sflist.size() > 0) {
        	this.saveBatchList(sflist);
        }
        if(rflist.size() > 0) {
        	toolExpressReconciliationRfService.saveBatchList(rflist);
        }
		return ResponseData.success();
	}

    @Resource
    private ToolExpressReconciliationRfService toolExpressReconciliationRfService;

	@Override
	public Workbook exportToExcel(Long reconciliationId) {
	    //这是表头
	    String[] arr = {"序号","日期","邮件号","到件地区","对方公司名称","计费重量","快递重量区间","产品","付款方式","费用","折扣","增值服务费","应付金额","经办人","备注"
	    		,"客户","总快递成本","增值服务费","快递费","首重","续重","折扣","成本差异","重量区间","收入"
	    		,"毛利","去税利润"};
	    //这是具体数据
	    List<List<String>> list = new ArrayList<>();
	    
	    ToolExpressReconciliationSfParam param = new ToolExpressReconciliationSfParam();
	    param.setReconciliationId(reconciliationId);
	    List<ToolExpressReconciliationSfResult> datalist = this.findListBySpec(param);
	    for (ToolExpressReconciliationSfResult r : datalist) {
		    List<String> list1 = new ArrayList<String>();
		    list1.add(r.getNoNumber());
		    list1.add(r.getReceivingTime());
		    list1.add(r.getOrderNumber());
		    list1.add(r.getReachArea());
		    list1.add(r.getDuifang());
		    list1.add(r.getChargingWeight());
		    list1.add(r.getWeightQj());
		    list1.add(r.getProduct());
		    list1.add(r.getFukuantype());
		    list1.add(r.getFeiyong());
		    list1.add(r.getDiscount());
		    list1.add(r.getZengzhi());
		    list1.add(r.getPostage());
		    list1.add(r.getJingbanren());
		    list1.add(r.getRemarks2());
		    
		    list1.add(r.getCustomerName());
		    list1.add(r.getPostageCheckSum());
		    list1.add(r.getZengzhi2());
		    list1.add(r.getPostageCheck());
		    list1.add(r.getFirstWeight());
		    list1.add(r.getWeightContinuation());
		    list1.add(r.getDiscount2());
		    list1.add(r.getPostageDifference());
		    list1.add(r.getWeightQj2());
		    list1.add(r.getIncome());
		    list1.add(r.getProfit());
		    list1.add(r.getProfitqs());
		    list.add(list1);
		}
	    return ExcelWrite.writeToExcelByList(arr, list);
	}

	@Override
	public void deleteBySpec(ToolExpressReconciliationSfParam param) {
		this.baseMapper.deleteBySpec(param);
	}

	@Override
	public boolean saveBatchList(List<ToolExpressReconciliationSfParam> list) {
		return this.baseMapper.saveBatchList(list);
	}

}
